package j2EE.servlet;


import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with Intel IDEA.
 * 将带有“XML-Encoded”的头部变量页面从XML转换为HTML的过滤器
 * @Auther: gjt
 * @Date: 2024/9/1 14:03
 */
public class XSLTFilter implements Filter {
    private FilterConfig filterConfig = null;

    public XSLTFilter (){}

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletContext servletContext = filterConfig.getServletContext();
        servletContext.log("XSLTFilter:doFilter() : start");

        // 包装响应（自己建的类）
        XSLTResponseWrapper xsltResponseWrapper = new XSLTResponseWrapper((HttpServletResponse) servletResponse, "/SimpleTransform.xsl");

        // 转发给下一个过滤器
        filterChain.doFilter( servletRequest, xsltResponseWrapper);

        // 写入实际响应(对结果进行处理)
        xsltResponseWrapper.writeResponse();
        servletContext.log("XSLTFilter:doFilter():end");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
